﻿using Admin.ApplicationLib.Services;
using Admin.CommonLib.Constants;
using Admin.CommonLib.Models.Role;
using Admin.CommonLib.Models.User;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Swimj.AspNetCore.Mvc;

namespace Admin.WebApi.Controllers
{
    [Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
    [ApiController]
    public class RoleController : ControllerBase
    {
        private readonly IRoleService _roleService;

        public RoleController(IRoleService roleService)
        {
            _roleService = roleService;
        }

        /// <summary>
        /// 查询角色列表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpGet(ApiRoutes.Role.QueryRoles)]
        [Authorize(Policy = nameof(ApiRoutes.Role.QueryRoles))]
        public async Task<IActionResult> Get([FromQuery] RoleSearchRequestDto model)
        {
            if (model.Page > 0 || model.Limit > 0)
            {
                var data = await _roleService.QueryRolesByPageAsync(model);
                return Ok(data);
            }
            else
            {
                var data = await _roleService.QueryRolesAsync(model);
                return Ok(data);
            }
        }

        /// <summary>
        /// 查询角色明细
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet(ApiRoutes.Role.GetRole)]
        [Authorize(Policy = nameof(ApiRoutes.Role.UpdateRole))]
        public async Task<IActionResult> Get(Guid id)
        {
            var data = await _roleService.GetRoleAsync(id);
            return Ok(data);
        }

        /// <summary>
        /// 创建角色
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost(ApiRoutes.Role.CreateRole)]
        [Authorize(Policy = nameof(ApiRoutes.Role.CreateRole))]
        public async Task<IActionResult> Post(RoleEditRequestDto model)
        {
            var data = await _roleService.CreateRoleAsync(model);
            return Ok(data);
        }

        /// <summary>
        /// 修改角色
        /// </summary>
        /// <param name="id"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPut(ApiRoutes.Role.UpdateRole)]
        [Authorize(Policy = nameof(ApiRoutes.Role.UpdateRole))]
        public async Task<IActionResult> Put(Guid id, RoleEditRequestDto model)
        {
            var data = await _roleService.UpdateRoleAsync(id, model);
            return Ok(data);
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete(ApiRoutes.Role.DeleteRole)]
        [Authorize(Policy = nameof(ApiRoutes.Role.DeleteRole))]
        public async Task<IActionResult> Delete(Guid id)
        {
            var data = await _roleService.DeleteRoleAsync(id);
            return Ok(data);
        }

        /// <summary>
        /// 导出角色列表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpGet(ApiRoutes.Role.ExportRole)]
        [Authorize(Policy = nameof(ApiRoutes.Role.ExportRole))]
        public async Task<IActionResult> Export([FromQuery] RoleSearchRequestDto model)
        {
            var result = await _roleService.ExportAsync(model);
            return new CustomFileResult(result.Item1, result.Item2);
        }

        /// <summary>
        /// 导出角色下载模板
        /// </summary>
        /// <returns></returns>
        [HttpGet(ApiRoutes.Role.ExportTemplateRole)]
        [Authorize(Policy = nameof(ApiRoutes.Role.ImportRole))]
        public async Task<IActionResult> ExportTemplate()
        {
            var result = await _roleService.ExportTemplateAsync();
            return new CustomFileResult(result.Item1, result.Item2);
        }

        /// <summary>
        /// 导入角色
        /// </summary>
        /// <param name="importType"></param>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost(ApiRoutes.Role.ImportRole)]
        [Authorize(Policy = nameof(ApiRoutes.Role.ImportRole))]
        public async Task<IActionResult> Import([FromQuery] int importType, IFormFile file)
        {
            var stream = file.OpenReadStream();

            byte[] bytes = new byte[stream.Length];
            var _ = await stream.ReadAsync(bytes, 0, bytes.Length);
            //设置当前流的位置为流的开始
            stream.Seek(0, SeekOrigin.Begin);

            MemoryStream memoryStream = new MemoryStream(bytes);
            var result = await _roleService.ImportAsync(importType, memoryStream);
            return Ok(result);
        }

        /// <summary>
        /// 查询角色下的所有权限信息
        /// </summary>
        /// <param name="id">角色标识</param>
        /// <returns></returns>
        [HttpGet(ApiRoutes.Role.QueryPermissions)]
        public async Task<IActionResult> GetPermissionsByRoleId(Guid id)
        {
            var data = await _roleService.QueryPermissionsByRoleIdAsync(id);
            return Ok(data);
        }

        /// <summary>
        /// 修改角色下的权限关联信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPut(ApiRoutes.Role.UpdatePermissions)]
        public async Task<IActionResult> PutPermissionsByRoleId(Guid id, List<string> model)
        {
            var data = await _roleService.UpdatePermissionsByRoleIdAsync(id, model);
            return Ok(data);
        }

        /// <summary>
        /// 查询角色下的所有用户信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpGet(ApiRoutes.Role.QueryUsers)]
        public async Task<IActionResult> GetUsersByRoleId(Guid id, [FromQuery] UserSearchRequestDto model)
        {
            var data = await _roleService.QueryUsersByRoleIdAsync(id, model);
            return Ok(data);
        }

        /// <summary>
        /// 修改角色下的用户关联信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPut(ApiRoutes.Role.UpdateUsers)]
        public async Task<IActionResult> PutUsersByRoleId(Guid id, RoleOperateRequestDto model)
        {
            var data = await _roleService.UpdateUsersByRoleIdAsync(id, model);
            return Ok(data);
        }


    }
}